<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="70" label-position="right">
        <FormItem label="线路">
            <Input type="text" v-model="formValidate.title"></Input>
        </FormItem>
        <FormItem label="团号">
            <Input type="text" v-model="formValidate.sn"></Input>
        </FormItem>
        <FormItem label="人数">
            <Input type="text" v-model="formValidate.num"></Input>
        </FormItem>
        <FormItem label="出发日期">
            <Input type="text" v-model="formValidate.start_date"></Input>
        </FormItem>
        <FormItem label="回程日期">
            <Input type="text" v-model="formValidate.end_date"></Input>
        </FormItem>
        <FormItem label="所属">
            <Select placeholder="选择所属" v-model="formValidate.category_id">
                <Option v-for="item in categoryList" :key="item.key" :value="item.id">{{ item.title }}</Option>                
            </Select>
        </FormItem>
        <FormItem label="部门">
           <Select placeholder="选择部门" v-model="formValidate.branch_id">
                <Option v-for="item in branchList" :key="item.key" :value="item.id">{{ item.title }}</Option>                                   
            </Select>
        </FormItem>
        <FormItem label="联系人">
            <Input type="text" v-model="formValidate.link_info"></Input>
        </FormItem>
        <FormItem label="导游">
           <Select placeholder="选择导游" v-model="formValidate.guide_id">
                <Option v-for="item in guideList" :key="item.key" :value="item.id">{{ item.name }}</Option>                
            </Select>
        </FormItem>
        <FormItem label="业务员">
            <Select placeholder="选择业务员" v-model="formValidate.salesman_id">
                <Option v-for="item in mangerList" :key="item.key" :value="item.id">{{ item.cn_name }}</Option>                
            </Select>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">确定</Button>
        </FormItem>
    </Form>
</template>
<script>
import { getCategoryList,getManagerList,getBranchList,getGuideList } from '@/api/data'

export default {
    name: 'addteam',    
    data(){
        return {  
            categoryList: [],
            mangerList: [],
            guideList: [],
            branchList: [],
            formValidate: {},
            ruleValidate: {
                company_name:[
                    { required: true, message: '请输入公司名称', trigger: 'blur' }
                ],
                mechanism_code:[
                    { required: true, message: '请输入机构代码', trigger: 'blur' }
                ]
            }       
        }
    },
    created(){   
        getCategoryList(1).then(res => {
            this.categoryList = res.data.datas
        });
        getManagerList().then(res => {
            this.mangerList = res.data.datas
        })
        getGuideList().then(res => {
            this.guideList = res.data.datas
        })
        getBranchList().then(res => {
            this.branchList = res.data.datas
        })
    },
    methods: { 
        addClient(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/client/create',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('录入成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('录入失败!');
            });   
        },
        editClient(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/client/update',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('编辑成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('编辑失败!');
            });   
        },                 
        handleSubmit(name){  
            this.$refs[name].validate((valid) => {                   
                if (valid) {                                 
                    if(this.formValidate.id != ""){
                        this.editClient(); 
                    }else{
                        this.addClient();                                 
                    }  
                    this.$emit('updateClientList');    
                    this.$emit('closeDialog');              
                } else {
                    this.$Message.error('提交失败!');
                }
            })
        }
    }
}
</script>
